package freespiritsocial.io.user.weight

import android.content.Context
import android.os.Bundle
import android.text.Html
import android.text.method.ScrollingMovementMethod
import android.view.Gravity.CENTER
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import freespiritsocial.io.user.R

class MyDialog(
    val mContext: Context,
    val title: String = "",
    val message: String = "",
    val hintText: String = "",
    val confirm: String = "Confirm",
    val cancel: String = "Cancel",
    val intputType: Int = EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
    val messageGravity: Int = CENTER,
    val isOneBtn: Boolean = false,
    val autoDismiss:Boolean = true,
    val callBack: (String) -> Unit
) : AlertDialog(mContext) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
    }

    private var et: EditText? = null

    fun getEditText(): String {
        return et?.text.toString()
    }


    private fun init() {
        setCancelable(false)
        setContentView(
            LayoutInflater.from(mContext)
                .inflate(R.layout.layout_dialog, null)
                .apply {
                    findViewById<TextView>(R.id.tv_message)?.apply {
                        isVisible = !message.isNullOrEmpty()
                        text = Html.fromHtml(message)
                        movementMethod = ScrollingMovementMethod.getInstance()
                        gravity = messageGravity
                    }

                    findViewById<TextView>(R.id.tv_title)?.apply {
                        isVisible = !title.isNullOrEmpty()

                        text = title
                    }

                    findViewById<EditText>(R.id.ev_input)?.apply {
                        et = this
                        inputType = intputType
                        visibility =
                            if (!hintText.isNullOrEmpty())
                                View.VISIBLE
                            else
                                View.GONE

                        hint = hintText
                    }
                    findViewById<View>(R.id.divider)?.isVisible = isOneBtn.not()
                    findViewById<TextView>(R.id.tv_cancel)?.apply {

                        text = cancel
                        isVisible = isOneBtn.not()
                        setOnClickListener {
                            et?.setText("")
                            dismiss()
                        }
                    }
                    findViewById<TextView>(R.id.tv_confirm)?.apply {
                        text = confirm
                        setOnClickListener {
                            if (autoDismiss)
                                dismiss()
                            callBack.invoke(getEditText())
                            et?.setText("")
                        }
                    }


                },
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

        )
//        (view.parent as ViewGroup).setBackgroundResource(android.R.color.transparent)

        window!!.attributes.let {
            it.width = window!!.windowManager.defaultDisplay.width.div(1.3f).toInt()
            it
        }
        window!!.setBackgroundDrawableResource(android.R.color.transparent)
        window!!.clearFlags(FLAG_ALT_FOCUSABLE_IM)
    }

    fun clearInput() {


    }

}